package com.wxx.zuoye.lagouspringboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.wxx.zuoye.lagouspringboot.pojo.TArticle;
import com.wxx.zuoye.lagouspringboot.service.imp.IndexServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class SelectController {
    @Autowired
    IndexServiceImp indexServiceImp;
    @RequestMapping("/baseindex")
    public String baseIndex(@RequestParam(value ="startpage",defaultValue = "1") int startpage,
                            @RequestParam(value ="num",defaultValue = "3") int num, Model model){
        //总条数
        long numTotal=0;
        //总页数
        long pageTotal=0;

        Iterable<TArticle> tArticlesList= indexServiceImp.selectTArticle();
        ArrayList<TArticle> tArticles = Lists.newArrayList(tArticlesList);
        pageTotal=tArticles.size()/num+1;
        List<TArticle>  pagetArticlest=null;
        if(num*startpage<tArticles.size()){
             pagetArticlest=tArticles.subList(num*(startpage-1),num*startpage);
        }else {
           pagetArticlest=tArticles.subList(num*(startpage-1),tArticles.size());
        }

        //PageInfo<TArticle> pageInfo = new PageInfo<TArticle>( Lists.newArrayList(tArticlesList));
       // PageHelper.startPage(startpage,num);
       // numTotal=pageInfo.getTotal();
       // pageTotal=pageInfo.getPageSize();
       //List<TArticle>  pagetArticlest=pageInfo.getList();
       model.addAttribute("tArticlesList",pagetArticlest);
        model.addAttribute("numTotal",tArticles.size());

        model.addAttribute("pageTotal",pageTotal);
        model.addAttribute("startpage",startpage);
        return "index";
    }
    @RequestMapping("/testheader")
    public String testHeader(){
        return "header";
    }
    @RequestMapping("/testfooter")
    public String testFooter(){
        return "footer";
    }
    @RequestMapping("/test")
    public String testIndex(){
        return "index";
    }
}
